<template>
  <div class="comments-container">
    <el-card>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>新闻中心</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="comments-content">
        <div class="item" v-for="(item,i) of tableData" :key="i">
          <div class="flex-row align-center head line50">
            <el-link href="javascript:;" class="font18"
                     @click="$router.push({name:'frontNewsInfo',query:{id:item.id}})">
              {{ item.title }}
            </el-link>
          </div>
          <div class="flex-row">
            <el-image
                alt=""
                class="cover-image"
                :key="item.cover"
                :src="item.cover"
                v-if="item.cover&&item.cover.trim().length"/>
            <div class="p_y20 font14 text-black p_x10 comment flex1">
              {{ item.content || '' }}
            </div>
          </div>
          <div class="flex-row justify-between align-center font12 text-light foot">
            <div class="time">{{ format(item.createTime) }}</div>
            <div class="goods-info">
              <el-button size="small" @click="$router.push({name:'frontNewsInfo',query:{id:item.id}})">
                查看详情
              </el-button>
            </div>
          </div>
        </div>
      </div>
      <div class="row-align-center footer">
        <div class="text-light font12" v-if="pageMeta.isEnd">---到底了---</div>
        <el-link class="" href="javascript:;" @click="nextPage" v-else>查看更多</el-link>
      </div>
    </el-card>

  </div>
</template>

<script>
import {defineComponent, reactive, ref, toRaw} from "vue"
import {deepClone} from "../utils/helpers";
import Toast from "../utils/Toast";
import News from "../api/modules/News";
import dayjs from "dayjs";
import {usePageMeta} from "../hooks/usePageTableData";

export default defineComponent({
  name: "News",
  methods: {
    format(time, layout = 'YYYY-MM-DD HH:mm:ss') {
      return dayjs(time).format(layout)
    }
  },
  setup() {
    const pageHandle = usePageMeta();
    const params = reactive({
      filter: "",
    });
    const tableData = ref([]);
    onPageChange(1); // 初始化时直接请求一次分类列表
    async function onPageChange(page) {
      const pageMeta = deepClone(toRaw(pageHandle.pageMeta.value));
      if (page) {
        pageMeta.index = page;
      }
      try {
        const _params = {
          ...pageMeta,
          ...params,
        };
        const {data} = await News.lists(_params);
        const _data = (data.data || []).map(v => {
          let content = {};
          try {
            content = JSON.parse(v.content) || {}
          } catch (e) {

          }
          return {
            ...v,
            ...content,
          }
        });
        if (_params.index > 1) {
          tableData.value.push(..._data);
        } else { // 第一页
          tableData.value = _data;
        }

        pageHandle.updatePageMeta(_params.index, data.count);

      } catch (e) {
        console.log(e);
      }
    }

    // 查看更多
    function nextPage() {
      if (pageHandle.pageMeta.value.isEnd) {
        Toast.warning('到底了');
        return
      }
      onPageChange(pageHandle.pageMeta.value.index + 1)
    }

    return {
      ...pageHandle,
      nextPage,
      tableData,
    }
  },
})
</script>

<style lang="scss" scoped>
.comments-content {
  padding: 20px 0;

  .cover-image {
    height: 100px;
    width: 200px;
    border-radius: 5px;
  }


  .item {
    margin-bottom: 10px;
    padding: 10px;
    border-bottom: solid 1px #f8f8f8;

    &:last-child {
      border-width: 0;
    }

    .head {
      .cover {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        border-width: 0;
      }
    }

    .comment {
      margin-bottom: 10px;
      height: 80px;
      overflow: hidden;
    }
  }
}

</style>
